package ex01.pyrmont;

import com.sky.JULLog.MyLogger;

import java.io.OutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.File;
import java.util.logging.Logger;

/**
 * HTTP ResponseV1 = Status-Line
 * (( general-header | response-header | entity-header ) CRLF)
 * CRLF
 * [ message-body ]
 * Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
 */
public class Response {
    private static Logger log = MyLogger.getLogger(Response.class);
    private static final int BUFFER_SIZE = 1398;
    Request request;
    OutputStream output;

    protected Response(OutputStream output) {
        this.output = output;
    }

    protected void setRequest(Request request) {
        this.request = request;
    }


    /**
     *
     * 返回 资源
     * */
    void sendStaticResource() throws IOException {
        byte[] bytes = new byte[BUFFER_SIZE];
        FileInputStream fis = null;
        try {
            File file = new File(TrashyTomcat.WEB_ROOT, this.request.getUri());   //windows  的路径需要变化
            if (file.exists()) {
                log.info("文件的 路径是 ==> " + file.getAbsolutePath());
                fis = new FileInputStream(file);
                int ch = fis.read(bytes, 0, BUFFER_SIZE);
                while (ch != -1) {
                    output.write("HTTP/1.1 200 OK\r\n".getBytes());
                    output.write("Connection: close\r\n".getBytes());
                    if (file.getName().endsWith("gif")) {
                        output.write("Content-Type: img/gif\r\n".getBytes());
                    } else {

                        output.write("Content-Type: text/html\r\n".getBytes());
                    }
                    output.write(("Content-Length: " + file.length() + "\r\n").getBytes());
                    output.write("\r\n".getBytes()); // 空行标识Header和Body的分隔
                    output.write(bytes, 0, ch);
                    ch = fis.read(bytes, 0, BUFFER_SIZE);
                }
            } else if (TrashyTomcat.SHUTDOWN_COMMAND.equals(request.getUri())) {

                String shutdownMsg = "HTTP/1.1 200 OK\r\n" +
                        "Content-Type: text/html\r\n" +
                        "Content-Length: " + "<h1>serverShutDown............</h1>".getBytes().length + "\r\n" +   //长度, 一定要对应上, 否则 Firefox 会读不完data ;
                        "\r\n" +
                        "<h1>serverShutDown............</h1>";   //内容
                output.write(shutdownMsg.getBytes());
            } else {
                // file not found
                String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
                        "Content-Type: text/html\r\n" +
                        "Content-Length: 23\r\n" +
                        "\r\n" +
                        "<h1>404 File Not Found </h1>";
                output.write(errorMessage.getBytes());
            }
        } catch (Exception e) {
            // thrown if cannot instantiate a File object
            System.out.println(e.toString());
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }
}
